
package gui.panels;

import coffee.CaffeineLevel;
import gui.MainFrame;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;

/**
 *
 * @author JtR
 */
public class ProfileContentPanel extends JPanel {
    private JPanel leftSide, rightSide;
    private JLabel userPic, textPercentage;
    private Image pic0, pic1, pic2, pic3, pic4, pic5, pic6, pic7, pic8, pic9, pic10;
    
    public ProfileContentPanel() {
        initComponents();
    }
    
    private void initComponents() {
        setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.gridx = GridBagConstraints.RELATIVE;
        c.gridy = 0;
        c.insets = new Insets(20, 20, 20, 20);
        c.weighty = 1;
        c.anchor = GridBagConstraints.WEST;
        leftSide = new JPanel(new GridBagLayout());
        add(leftSide, c);
        initLeftSide();
        
        c.weightx = 1;
        c.anchor = GridBagConstraints.NORTH;
        c.insets = new Insets(20, 0, 20, 20);
        rightSide = new JPanel(new GridBagLayout());
        add(rightSide, c);
        initRightSide();
        setBackground(Color.WHITE);
        
        updateFields();
    }
    
    private void initLeftSide() {
        try {
            GridBagConstraints c = new GridBagConstraints();
            leftSide.setBackground(Color.WHITE);
            leftSide.setBorder(new LineBorder(Color.BLACK));
            pic0 = ImageIO.read(this.getClass().getClassLoader().getResource("res/gesu/gesu_m_0.png"));
            pic1 = ImageIO.read(this.getClass().getClassLoader().getResource("res/gesu/gesu_m_10.png"));
            pic2 = ImageIO.read(this.getClass().getClassLoader().getResource("res/gesu/gesu_m_20.png"));
            pic3 = ImageIO.read(this.getClass().getClassLoader().getResource("res/gesu/gesu_m_30.png"));
            pic4 = ImageIO.read(this.getClass().getClassLoader().getResource("res/gesu/gesu_m_40.png"));
            pic5 = ImageIO.read(this.getClass().getClassLoader().getResource("res/gesu/gesu_m_50.png"));
            pic6 = ImageIO.read(this.getClass().getClassLoader().getResource("res/gesu/gesu_m_60.png"));
            pic7 = ImageIO.read(this.getClass().getClassLoader().getResource("res/gesu/gesu_m_70.png"));
            pic8 = ImageIO.read(this.getClass().getClassLoader().getResource("res/gesu/gesu_m_80.png"));
            pic9 = ImageIO.read(this.getClass().getClassLoader().getResource("res/gesu/gesu_m_90.png"));
            pic10 = ImageIO.read(this.getClass().getClassLoader().getResource("res/gesu/gesu_m_100.png"));
            
            userPic = new JLabel();
            
            c.gridx = 0;
            c.gridy = 0;
            c.weightx = 1;
            leftSide.add(userPic, c);
            
            
            c.insets = new Insets(20, 20, 20, 20);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    
    private void initRightSide() {
        GridBagConstraints c = new GridBagConstraints();
        rightSide.setBackground(Color.WHITE);
        
        c.gridx = 0;
        c.gridy = GridBagConstraints.RELATIVE;
        
        JLabel t1 = new JLabel("Sie haben einen Koffeinlevel von");
        JLabel t2 = new JLabel("Bitte halten Sie es stets im Auge.");
        Font f = t1.getFont().deriveFont(20f);
        textPercentage = new JLabel();
        
        rightSide.add(t1, c);
        c.insets = new Insets(20, 0, 0, 0);
        rightSide.add(textPercentage, c);
        rightSide.add(t2, c);
        
        t1.setFont(f);
        textPercentage.setFont(f);
        t2.setFont(f);
    }
    
    public void updateFields() {
        double percentage = CaffeineLevel.getCaffeineLevel(MainFrame.user.getWeight(), MainFrame.user.getCoffeinSum24()) * 100d;
        
        float temp = (float)percentage / 10f;
        int clamp = Math.round(temp);
        clamp = Math.max(0, Math.min(10, clamp));
        switch(clamp) {
            case 0:
                userPic.setIcon(new ImageIcon(pic0));
                break;
            case 1:
                userPic.setIcon(new ImageIcon(pic1));
                break;
            case 2:
                userPic.setIcon(new ImageIcon(pic2));
                break;
            case 3:
                userPic.setIcon(new ImageIcon(pic3));
                break;
            case 4:
                userPic.setIcon(new ImageIcon(pic4));
                break;
            case 5:
                userPic.setIcon(new ImageIcon(pic5));
                break;
            case 6:
                userPic.setIcon(new ImageIcon(pic6));
                break;
            case 7:
                userPic.setIcon(new ImageIcon(pic7));
                break;
            case 8:
                userPic.setIcon(new ImageIcon(pic8));
                break;
            case 9:
                userPic.setIcon(new ImageIcon(pic9));
                break;
            case 10:
                userPic.setIcon(new ImageIcon(pic10));
                break;
        }
        
        String text = new DecimalFormat("#0.00").format(percentage) + "%";
        textPercentage.setText(text);
    }
}
